package com.party.web.jms.listener;

import com.party.common.constant.Constant;
import com.party.core.exception.BusinessException;
import com.party.core.service.order.biz.OrderFormBizService;
import com.party.web.biz.order.QueryBizService;
import org.apache.activemq.command.ActiveMQMapMessage;
import org.apache.activemq.command.ActiveMQMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.Message;

/**
 * @author yifeng
 * @createTime 2018/1/30 0030 下午 15:31
 * @description 订单取消队列消息监听
 */
@Component(value = "orderCancelListener")
public class OrderCancelListener implements javax.jms.MessageListener {
    @Autowired
    private OrderFormBizService orderFormBizService;
    @Autowired
    private QueryBizService queryBizService;

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void onMessage(Message message) {
        try {
            System.out.println("消息接收时间：" + System.currentTimeMillis());
            if (message instanceof ActiveMQMessage) {
                ActiveMQMapMessage mqMessage = (ActiveMQMapMessage) message;
                String orderId = mqMessage.getString(Constant.TYPE_KEY);
                boolean result = queryBizService.checkOrderTradeStatus(orderId);
                if (result) {
                    orderFormBizService.orderOvertimeCancel(orderId);
                }
            }
        } catch (BusinessException e) {
            logger.error(e.getMessage(), e);
        } catch (Exception e) {
            logger.error("超时订单取消消息队列异常", e);
        }
    }
}
